package com.github.tvbox.osc.ui.tv.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.github.tvbox.osc.R;

import java.util.List;

/**
 * 自定义内容轮播
 */
@Deprecated
public class BannerViewPage extends LinearLayout {

    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 图片轮播视图
     */
    private ViewPager mAdvPager = null;
    /**
     * 滚动图片视图适配
     */
    private ViewCycleAdapter mAdvAdapter;
    private BannerViewPageSelectListener bannerViewPageSelectListener;
    private int count = 0;

    public BannerViewPage(Context context) {
        super(context);
    }

    public BannerViewPage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_banner_viewpage, this);
        mAdvPager = (ViewPager) findViewById(R.id.adv_pager);
        mAdvPager.addOnPageChangeListener(new GuidePageChangeListener());
    }

    /**
     * 装填图片数据
     */
    public void setResource(List<View> mList, BannerViewPageSelectListener bannerViewPageSelectListener) {
        this.mAdvAdapter = new ViewCycleAdapter(mList);
        if (bannerViewPageSelectListener != null) {
            this.bannerViewPageSelectListener = bannerViewPageSelectListener;
        }
        this.count = mList.size();
        this.mAdvPager.setAdapter(mAdvAdapter);
        this.mAdvPager.setOffscreenPageLimit(0);
    }

    /**
     * 轮播图片监听
     */
    private final class GuidePageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int state) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int index) {
            bannerViewPageSelectListener.onItemSelect(index % count);
        }
    }

    private class ViewCycleAdapter extends PagerAdapter {
        private List<View> mView;

        public ViewCycleAdapter(List<View> mView) {
            this.mView = mView;
        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = mView.get(position % count);
            ViewGroup vp = (ViewGroup) view.getParent();
            if (vp != null) {
                vp.removeView(view);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

        }
    }

    /**
     * 轮播控件的监听事件
     */
    public interface BannerViewPageSelectListener {
        public void onItemSelect(int position);
    }

    public interface BannerViewPageObserver extends LifecycleObserver {
        void onStop(LifecycleOwner owner);

        void onStart(LifecycleOwner owner);

        void onDestroy(LifecycleOwner owner);
    }
}
